package com.zoke.support.image.activity;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;

import com.zoke.support.R;
import com.zoke.support.base.BaseActivity;
import com.zoke.support.image.ImageBrowserAdapter;
import com.zoke.support.pulltorefresh.lib.extras.viewpager.PullToRefreshViewPager;
import com.zoke.support.view.ZokeButton;
import com.zoke.support.view.ZokeTextView;

/**
 * 大图预览
 * 
 * @author JackWu
 * @version
 * @date 2014-10-29
 * @_QQ_ 651319154
 */
public class PhotoBrowserActivity extends BaseActivity implements
		OnPageChangeListener {

	protected PullToRefreshViewPager mPullContainerVP;
	private ZokeTextView mPhotosNumTv;
	private ZokeButton mSaveBtn;
	private ViewPager mViewPager;

	/** 数据 **/
	protected ArrayList<String> mPhotos = new ArrayList<String>();
	private int mCurrentIndex;

	/** ArrrayList<大图的下载地址>的key **/
	public static final String PHOTODATAS = "_photos";
	/** 当前显示哪张大图 **/
	public static final String CURRENTPOSITION = "_position";

	/** 适配器 **/
	private ImageBrowserAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_photosbrower);
		mPullContainerVP = (PullToRefreshViewPager) findViewById(R.id.pull_refresh_viewpager);
		mViewPager = mPullContainerVP.getRefreshableView();
		// 大图位置
		mPhotosNumTv = (ZokeTextView) findViewById(R.id.photobrower_nums_tv);
		// 存储按钮
		mSaveBtn = (ZokeButton) findViewById(R.id.photobrower_save_btn);
		// TODO:暂时不支持这个功能
		mSaveBtn.setVisibility(View.INVISIBLE);
		// 获取传值过来的UrlString集合
		ArrayList<String> list = getIntent().getStringArrayListExtra(PHOTODATAS);
		if(list!=null && list.size()!=0){
			mPhotos.addAll(list);
		}
		mCurrentIndex = getIntent().getIntExtra(CURRENTPOSITION, 0);
		setPageText(mCurrentIndex);
		// 设置viewpager保留界面不重新加载的页面数量
		mViewPager.setOffscreenPageLimit(3);
		// 默认是1,，自动预加载左右两边的界面。设置viewpager预加载数为0。只加载加载当前界面。
		mViewPager.setPrepareNumber(1);
		// 设置可以滑动
		mViewPager.setCanScroll(true);
		mAdapter = new ImageBrowserAdapter(getApplicationContext(), mPhotos,
				new PhotoTabFinishedListener() {
					@Override
					public void finishActivity() {
						photoFinished();
					}
				});
		mViewPager.setAdapter(mAdapter);
		// 直接跳转到
		mViewPager.setCurrentItem(mCurrentIndex, false);
		mViewPager.setOnPageChangeListener(this);
	}
	
	protected void notifyData(){
		if(mAdapter!=null){
			mAdapter.notifyDataSetChanged();
			if(mCurrentIndex+1 == mPhotos.size()){
				return;
			}
			mCurrentIndex = mCurrentIndex+1;
			mViewPager.setCurrentItem(mCurrentIndex, false);
			setPageText(mCurrentIndex);
		}
	}

	@Override
	protected void handleMsg(Message msg) {
		super.handleMsg(msg);
	}

	/** 用户点击大图进行界面的关闭时，调用此方法 **/
	protected void photoFinished() {
		this.onBackPressed();
	}

	@Override
	public void onPageScrolled(int position, float positionOffset,
			int positionOffsetPixels) {

	}

	@Override
	public void onPageSelected(int position) {
		mCurrentIndex = position;
		setPageText(position);
	}

	@Override
	public void onPageScrollStateChanged(int state) {

	}

	/** 设置底部页面数 **/
	protected void setPageText(int position) {
		mPhotosNumTv.setText((position + 1) + "/" + mPhotos.size());
	}

	/** 大图点击销毁界面 **/
	public interface PhotoTabFinishedListener {
		public void finishActivity();
	}

}
